﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Model.EntityModel;

namespace DataAccess
{
    public static class DeclarationDataAccess
    {
        public static Declaration InsertDeclaration(Declaration declaration)
        {
            BrigadaEntities context = new BrigadaEntities();
            context.Declaration.AddObject(declaration);
            context.SaveChanges();
            return declaration;
        }

        public static Declaration UpdateDeclaration(Declaration declaration)
        {
            BrigadaEntities context = new BrigadaEntities();
            var decl = (from elem in
                            context.Declaration
                        where elem.Id == declaration.Id
                        select elem).SingleOrDefault();
            decl.Answer = declaration.Answer;
            decl.Question = declaration.Question;
            decl.ActorId = declaration.ActorId;
            decl.RegistrationDate = declaration.RegistrationDate;
            decl.Status = declaration.Status;
            context.SaveChanges();
            return decl;
        }

        public static List<ShowableDeclaration> GetAllShowableDeclarations(long complaintId)
        {
            BrigadaEntities context = new BrigadaEntities();
            var query = from elem in
                            context.Declaration
                        where elem.ComplaintId == complaintId 
                        select elem;
            List<Declaration> declarations = query.ToList();
            List<ShowableDeclaration> showableDeclarations = new List<ShowableDeclaration>();
            foreach (var declaration in declarations)
            {
                int index = GetIndexIfDeclarationInList(declaration.RegistrationDate,
                                                        declaration.Actor.Person.CiPassport, showableDeclarations);
                if (index == -1)
                {
                    var questions = new List<ShowableQuestion>();
                    questions.Add(new ShowableQuestion{ NumberOfQuestion = 1, DeclarationId = declaration.Id,Question = declaration.Question, Answer = declaration.Answer});
                    showableDeclarations.Add(new ShowableDeclaration{ActorType = declaration.Actor.Type, DeclarantCI = declaration.Actor.Person.CiPassport, RegistrationDate = declaration.RegistrationDate.ToShortDateString(),FullName = declaration.Actor.Person.Name +
                    " "+declaration.Actor.Person.PaternalName + " " +declaration.Actor.Person.MaternalName,QuestionsAndAnswer = questions});
                }
                else
                {
                    showableDeclarations[index].QuestionsAndAnswer.Add(new ShowableQuestion { NumberOfQuestion = showableDeclarations[index].QuestionsAndAnswer.Count+1, DeclarationId = declaration.Id, Question = declaration.Question, Answer = declaration.Answer });
                }
            }
            return showableDeclarations;
        }

        private static int GetIndexIfDeclarationInList(DateTime dateTime, long actorCi, List<ShowableDeclaration> declarations)
        {
            for (int i = 0; i < declarations.Count; i++)
            {
                if (declarations[i].RegistrationDate==dateTime.ToShortDateString() && declarations[i].DeclarantCI==actorCi)
                {
                    return i;
                }
            }
            return -1;
        }

        public static List<ShowableDeclaration> GetDeclarationsBy(List<ShowableDeclaration> declarations, string byActor)
        {
            return (from elem in
                        declarations
                   where elem.ActorType == byActor
                   select elem).ToList();
        }

        public static Declaration GetDeclarationsById(long declarationId)
        {
            BrigadaEntities context = new BrigadaEntities();
            return (from elem in
                        context.Declaration
                    where elem.Id == declarationId
                    select elem).FirstOrDefault();
        }
    }
}
